<?php


namespace Littlexiaoshuishui\Upay;


use Littlexiaoshuishui\Upay\Abstracts\GatewayInterface;
use Littlexiaoshuishui\Upay\Gateways\Qfpay\Support;

class Pay
{
    protected $config;

    public function __construct(array $config)
    {
        $this->config = $config;
    }

    /**
     * Magic static call.
     * @param string $method
     * @param array  $params
     */
    public static function __callStatic($method, $params)
    {
        $app = new self(...$params);

        return $app->create($method);
    }

    /**
     * Create a instance.
     * @param string $method
     *
     */
    protected function create($method)
    {
        $gateway = __NAMESPACE__.'\\Gateways\\'.Support::namePase($method);

        if (class_exists($gateway)) {
            return self::make($gateway);
        }
        throw new PayException(PayException::INVALID_PARAMS);
    }

    /**
     * Make a gateway.
     *
     * @param string $gateway
     *
     */
    protected function make($gateway)
    {
        $app = new $gateway($this->config);

        if ($app instanceof GatewayInterface) {
            return $app;
        }

        throw new PayException(PayException::INVALID_PARAMS,"Gateway [{$gateway}] Must Be An Instance Of GatewayApplicationInterface");
    }


}